module;
#include <QObject>
#include <QMap>
#include <QString>
#include <QMutex>
#include <QtCore>
#include <QtCore/qtmochelpers.h>
#include <QtCore/QPair>
#include <QThread>
module OpcUaClientThread;
import XYopcUa;

OpcUaClientThread::OpcUaClientThread(QObject *parent)
    : QThread(parent)
    , m_client(new OpcUaClient(this))
{
}

OpcUaClientThread::~OpcUaClientThread()
{
    quit();
    wait();

    // Disconnect if connected
    if (m_client) {
        m_client->disconnectFromEndpoint();
        delete m_client;
        m_client = nullptr;
    }
}

void OpcUaClientThread::run()
{
    // Connect thread signals to client slots
    connect(this, &OpcUaClientThread::connectToEndpoint,
            m_client, &OpcUaClient::connectToEndpointSlot);
    connect(this, &OpcUaClientThread::disconnectFromEndpoint,
            m_client, &OpcUaClient::disconnectFromEndpointSlot);
    connect(this, &OpcUaClientThread::readNode,
            m_client, &OpcUaClient::readNodeSlot);
    connect(this, &OpcUaClientThread::readNodes,
            m_client, &OpcUaClient::readNodesSlot);
    connect(this, &OpcUaClientThread::writeNode,
            m_client, &OpcUaClient::writeNodeSlot);
    connect(this, &OpcUaClientThread::writeNodes,
            m_client, &OpcUaClient::writeNodesSlot);

    // Forward client signals
    connect(m_client, &OpcUaClient::connected,
            this, &OpcUaClientThread::connected);
    connect(m_client, &OpcUaClient::nodesRead,
            this, &OpcUaClientThread::nodesRead);
    connect(m_client, &OpcUaClient::nodesWritten,
            this, &OpcUaClientThread::nodesWritten);
    connect(m_client, &OpcUaClient::disconnected,
            this, &OpcUaClientThread::disconnected);
    connect(m_client, &OpcUaClient::errorOccurred,
            this, &OpcUaClientThread::errorOccurred);
    connect(m_client, &OpcUaClient::nodeValueRead,
            this, &OpcUaClientThread::nodeValueRead);
    connect(m_client, &OpcUaClient::nodeValueWritten,
            this, &OpcUaClientThread::nodeValueWritten);

    exec();
}



//放在文件最后，文件需要手动生成
#include "opcUaClientThread.moc"
